<template>
  <div class="customer-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>客户管理</span>
          <el-button type="primary" @click="addCustomer">新增客户</el-button>
        </div>
      </template>

      <el-form :model="queryParams" label-width="100px" style="margin-bottom: 20px;">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="客户名称">
              <el-input v-model="queryParams.name" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系人">
              <el-input v-model="queryParams.contact" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="电话">
              <el-input v-model="queryParams.phone" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button type="primary" @click="search">搜索</el-button>
            <el-button @click="reset">重置</el-button>
          </el-col>
        </el-row>
      </el-form>

      <el-table :data="customers" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="50" />
        <el-table-column prop="name" label="客户名称" />
        <el-table-column prop="contact" label="联系人" />
        <el-table-column prop="phone" label="联系电话" />
        <el-table-column prop="address" label="地址" />
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ statusMap[row.status] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button size="small" @click="editCustomer(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteCustomer(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-model:current-page="queryParams.page"
        v-model:page-size="queryParams.size"
        :total="total"
        @current-change="getList"
        layout="prev, pager, next"
        style="margin-top: 20px;"
      />
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="isEdit ? '编辑客户' : '新增客户'" width="50%">
      <el-form ref="formRef" :model="formData" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="客户名称">
              <el-input v-model="formData.name" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人">
              <el-input v-model="formData.contact" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话">
              <el-input v-model="formData.phone" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址">
              <el-input v-model="formData.address" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-select v-model="formData.status" placeholder="请选择" style="width: 100%">
                <el-option label="启用" :value="1" />
                <el-option label="停用" :value="0" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item>
          <el-button type="primary" @click="saveCustomer">保存</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'

const customers = ref([])
const total = ref(0)
const queryParams = ref({
  page: 1,
  size: 10,
  name: '',
  contact: '',
  phone: ''
})

const statusMap = {
  0: '停用',
  1: '启用'
}

const dialogVisible = ref(false)
const isEdit = ref(false)
const formData = ref({
  id: null,
  name: '',
  contact: '',
  phone: '',
  address: '',
  status: 1
})

const formRef = ref(null)

const getList = async () => {
  try {
    const res = await request.get('/customer/', { params: queryParams.value })
    customers.value = res.records
    total.value = res.total
  } catch (error) {
    console.error('获取客户列表失败:', error)
  }
}

const addCustomer = () => {
  isEdit.value = false
  formData.value = {
    name: '',
    contact: '',
    phone: '',
    address: '',
    status: 1
  }
  dialogVisible.value = true
}

const editCustomer = (row) => {
  isEdit.value = true
  formData.value = { ...row }
  dialogVisible.value = true
}

const deleteCustomer = async (id) => {
  try {
    await request.delete(`/customer/${id}/`)
    ElMessage.success('删除成功')
    getList()
  } catch (error) {
    ElMessage.error('删除失败')
    getList()
  }
}

const saveCustomer = async () => {
  try {
    if (isEdit.value) {
      // 编辑
      const res = await request.put(`/customer/${formData.value.id}/`, formData.value)
      ElMessage.success('更新成功')
      getList()
    } else {
      // 新增
      const res = await request.post('/customer/', formData.value)
      ElMessage.success('创建成功')
      getList()
    }
    dialogVisible.value = false
    getList()
  } catch (error) {
    ElMessage.error('操作失败')
  }
}

const search = () => {
  queryParams.value.page = 1
  getList()
}

const reset = () => {
  queryParams.value = {
    page: 1,
    size: 10,
    name: '',
    contact: '',
    phone: ''
  }
  getList()
}

onMounted(() => {
  getList()
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
